import java.sql.*;
import java.util.Scanner;

import com.mysql.cj.protocol.Resultset;
import com.mysql.jdbc.*;
import com.mysql.cj.*;

public class LoginTrail2 {
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://127.0.0.1/android_database"; //choose database
    static final String USER = "root";
    static final String PASSWORD = "225107";

    public static void main(String[] args){
        Login();
    }

    public static void Login(){
        System.out.println("Please input your Student ID:");
        Scanner scan = new Scanner(System.in);
        String ID = scan.nextLine();
        System.out.println("PLease input your Password");
        String Password = scan.nextLine();
        ReferSQL(ID, Password);
    }

    public static void ReferSQL(String mSno, String mPass){
        Connection conn = null;
        Statement stmt = null;
        try{
            Class.forName(JDBC_DRIVER);
            System.out.println("connecting to DB...please wait");
            conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
            System.out.println("Statement...please wait");
            stmt = conn.createStatement(); //connecting and create a statement

            String sql;
            sql = "SELECT Sno, Sname, Smajor, Sclass, Spass FROM student"; //choose table
            ResultSet rs = stmt.executeQuery(sql); //connect+statement+query

            int i = 0;
            int count = 0;
            while(rs.next()){
                count++;
            }

            ResultSet rs1 = stmt.executeQuery(sql);
            while(rs1.next()){
                i++;
                String TestID = rs1.getString("Sno");
                String TestName = rs1.getString("Sname");
                String TestMajor = rs1.getString("Smajor");
                String TestClass = rs1.getString("Sclass");
                String TestPass = rs1.getString("Spass");

                if (TestID.equals(mSno) && TestPass.equals(mPass)){
                    System.out.println("the result is:");
                    System.out.println("ID:"+TestID);
                    System.out.println("name:"+TestName);
                    System.out.println("major:"+TestMajor);
                    System.out.println("class:"+TestClass);
                    break;
                }
                if (TestID.equals(mSno) && !TestPass.equals(mPass)){
                    System.out.println("Wrong Password, please try again");
                    break;
                }
                if (i == count && !TestID.equals(mSno)){
                    System.out.println("Sorry, no such student");
                    break;
                }
            }

            rs.close();
            rs1.close();
            stmt.close();
            conn.close();
        }
        catch(SQLException se){
            se.printStackTrace();
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally {
            try{
                if (stmt != null) stmt.close();
            }catch(SQLException se){}
            try{
                if (conn != null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        System.out.println("That's the ending of RefSQL");
    }



}

